package com.ymatou.iapi.godmch.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.GetRoleUseTimesBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.GetRoleUseTimesCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
/**
*统计角色被客服使用次数
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetRoleUseTimes{
	private static GetRoleUseTimesBean getroleusetimesBean;
	private static GetRoleUseTimesCall getroleusetimesCall;
	Map<Integer, Integer> testmap;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("统计角色被客服使用次数");
	}

	@Before
	public void caseUp() {
		getroleusetimesBean=new GetRoleUseTimesBean();
		getroleusetimesCall=new GetRoleUseTimesCall();
		testmap=new HashMap<Integer, Integer>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getroleusetimesCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 创建客服返回客服id
	 * @param mchId
	 * @param templateId
	 * @return
	 * @throws Exception
	 */
	private Integer addCs(Integer mchId,Integer templateId) throws Exception {
		UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
		int csId=DGUserCallService.UserCreateCall(usercreateBean);
        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
        Logger.comment("创建渠道商客服:"+csId);
        if(testmap.containsKey(templateId)) {
        	testmap.put(templateId, testmap.get(templateId)+1);
        }
        else {
        	testmap.put(templateId, 1);
        }
        return csId;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleUseTimes_001() {
		Logger.start(true,"统计角色被客服使用次数,成功-有客服templateId1");
		try {
			//计数
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 1);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRoleUseTimes_002() {
		Logger.start(true,"统计角色被客服使用次数,成功-有多个不同角色客服");
		try {
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 1);
	        addCs(mchId, 2);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(2,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRoleUseTimes_003() {
		Logger.start(true,"统计角色被客服使用次数,成功-删除的客服不被统计");
		try {
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 1);
	        Integer csId = addCs(mchId, 1);
	        GodmchManageCallService.RemoveCs(mchId, csId);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			//移除的不算
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),1,"验证使用次数");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRoleUseTimes_004() {
		Logger.start(true,"统计角色被客服使用次数,不输入mchId失败");
		try {
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("4001", getroleusetimesCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleUseTimes_005() {
		Logger.start(true,"统计角色被客服使用次数,查询2个权限,其中1个有使用次数一个没有");
		try {
			getroleusetimesBean.setMchId(500001293);
			getroleusetimesBean.setRoleIds(Arrays.asList(1,2,3,4));
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleUseTimes_006() {
		Logger.start(true,"统计角色被客服使用次数,成功-有客服templateId2");
		try {
			//计数
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 2);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleUseTimes_007() {
		Logger.start(true,"统计角色被客服使用次数,成功-有客服templateId3");
		try {
			//计数
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 3);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleUseTimes_008() {
		Logger.start(true,"统计角色被客服使用次数,成功-有客服templateId4");
		try {
			//计数
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 4);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRoleUseTimes_009() {
		Logger.start(true,"统计角色被客服使用次数,成功-有多个同角色客服");
		try {
			int mchId=GodmchManageCallService.CreateSellerMch();
	        addCs(mchId, 1);
	        addCs(mchId, 1);
			getroleusetimesBean.setMchId(mchId);
			getroleusetimesBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getroleusetimesCall.setData(getroleusetimesBean);
			getroleusetimesCall.callService();			
			Logger.verifyEquals("200", getroleusetimesCall.getString("status"), "验证status");
			Logger.verifyEquals(1,getroleusetimesCall.getRoleUseTimesDto().size(), "验证数据条数");
			getroleusetimesCall.getRoleUseTimesDto().forEach(r->{
				Logger.verifyEquals(r.getUseTimes(),testmap.get(r.getRoleId()),"验证使用次数");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}